﻿using AvaloniaCommunityMvvmTemplate.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 

namespace AvaloniaCommunityMvvmTemplate.ViewModels
{
    public partial class MainWindowViewModel  
    {

        void BasicDemoInit()
        {
            People.Add(new People());
            People.Add(new People());
            People.Add(new People());
            People.Add(new People());
        }


        #region  basic demo 

        [ObservableProperty]//mvvm属性
        private int _score = 0;

        [ObservableProperty]//mvvm属性 可由生成器自动生成属性代码
        //[NotifyPropertyChangedFor(nameof(Caption))]//属性改变后通知执行
        private string _textValue = string.Empty;

        public string Caption => $"s:{TextValue}";

        partial void OnTextValueChanged(string value)
        {
            OnPropertyChanged(nameof(Caption));
        }


        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(ButtonClickCommand))]
        [NotifyCanExecuteChangedFor(nameof(AsyncButtonClickCommand))]//属性改变后统治方法执行CanExecute
        private bool _isEnable;


        /// <summary>
        /// 控制按钮的可用
        /// </summary>
        /// <returns></returns>
        bool CanButtonClick() => IsEnable;                   
        [RelayCommand(CanExecute = nameof(CanButtonClick))]
        public void ButtonClick()
        {
            Debug.WriteLine("ButtonClick");
            Score += 1;
            TextValue += $"clicked{Score} ";
        }

        /// <summary>
        /// 用于较为耗时的任务
        /// </summary>
        /// <returns></returns>
        [RelayCommand(CanExecute = nameof(CanButtonClick))]
        public async Task AsyncButtonClick()
        {
            await Task.Delay(2000);
            Debug.WriteLine("ButtonClick");
            Score += 1;
            TextValue += $"clicked{Score} ";
        }


        [ObservableProperty]
        private ObservableCollection<People> _people = new ObservableCollection<People>();

        /// <summary>
        /// 传参方法
        /// </summary>
        /// <param name="obj"></param>
        [RelayCommand]
        public void GetSelectItems(object obj)
        {
            List<People> peoples = new List<People>();
            int count = 0;
            foreach (var item in (IList)obj)
            {
                count++;
                peoples.Add((People)item);
            }
            TextValue = peoples.Count().ToString();
        }


        #endregion
    }
}
